package org.haredot.service.impl;

import org.haredot.dto.LeftMenu;
import org.haredot.dto.Permission;
import org.haredot.dto.UserDetailsDTO;
import org.haredot.entity.Menu;
import org.haredot.entity.Role;
import org.haredot.mapper.MenuMapper;
import org.haredot.mapper.UserDetailsMapper;
import org.haredot.service.MenuService;
import org.haredot.vo.ResultVo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper ;

    @Resource
    private UserDetailsMapper userDetailsMapper;

    @Override
    public ResultVo<Menu> saveMenu(Menu menu) {

        Menu m = menuMapper.findMenuByName(menu.getName());

        if (m !=null) {
            return ResultVo.<Menu>fail("菜单名已存在").build();
        }
        // 保存到数据库中
        menuMapper.save(menu);

        return ResultVo.<Menu>ok().data(menu).build();
    }

    @Override
    public ResultVo<List<Menu>> queryMenus() {

        List<Menu> list = menuMapper.findAll() ;

        return ResultVo.<List<Menu>>ok()
                .data(list)
                .build();
    }

    @Override
    public ResultVo<Menu> modifyStatus(Map<String, Object> param) {
        Integer status = (Integer) param.get("status");

        if (status != 0 && status != 1) {
            return ResultVo.<Menu>fail("status状态的值只能是0或1").build();
        }
        Integer condition = status == 0 ? 1 : 0 ;

        param.put("condition", condition);

        // 获取 更新的条数
        int len = menuMapper.modifyStatus(param);

        if (len == 1) {
            return ResultVo.<Menu>ok().build();
        }
        return ResultVo.<Menu>fail("状态修改失败").build();
    }

    @Override
    public ResultVo<Menu> modifyMenu(Menu menu) {
        // 根据 Menu 名查询 该菜单是否已经存在
        Menu m = menuMapper.findMenuByName(menu.getName()) ;

        if (m != null && !Objects.equals(menu.getId(), m.getId())) {
            return ResultVo.<Menu>fail("菜单名已存在").build();
        }
        // 修改数据
        menuMapper.updateMenu(menu);

        return ResultVo.<Menu>ok().data(menu).build();
    }

    @Override
    public ResultVo<Menu> removeMenu(Long id) {
        int len = menuMapper.removeMenuById(id);
        if (len == 1) {
            return ResultVo.<Menu>ok().build();
        }
        return ResultVo.<Menu>fail("移除失败").build();
    }

    @Override
    public ResultVo<?> queryLeftMenus(String username) {
        // 查询 该用户 对应的所有 角色 、岗位、 部门等信息
        UserDetailsDTO userDetails = userDetailsMapper.findUserByUsername(username);
        // 获取 角色信息
        List<Role> roles = userDetails.getRoles();
        if (roles.isEmpty()) {
            return ResultVo.fail("当前用户未绑定任何角色").build();
        }
        // 判断 角色中是否 包含 超级管理员
        long count = roles.stream().filter(role -> "super-admin".equals(role.getRemark())).count();

        List<LeftMenu> leftMenus = null;

        if (count > 0) {
            // 如果是超级管理员
            leftMenus = menuMapper.findLeftAllMenus();
        }else{
             // 查询 对应角色 的权限列表
            leftMenus = menuMapper.findLeftMenusByRoleIds(roles);
        }

        return ResultVo.ok().data(leftMenus).build();
    }
}
